import Vue from 'vue'
import '@/cube-ui'
import App from '@/App.vue'
import router from '@/router'
import store from '@/store'
import axios from 'axios'
import VueLazyload from 'vue-lazyload'
import '@/tool/timefilter.js'
import '@/tool/load'

Vue.config.productionTip = false

// ========== axios ================ start
const http = axios.create({
  baseURL: 'http://localhost:8080/api',
  timeout: 4000
})
// ******全局 http 拦截器******
// ---- request ----
http.interceptors.request.use(config => {
  return config
}, err => {
  return Promise.reject(err)
})
// ---- response ----
http.interceptors.response.use(res => {
  return res
}, err => {
  // 全局超时处理
  if (err.message.includes('timeout')) {
    console.log('请求超时')
    // eslint-disable-next-line
    mui.toast('请求超时')
  } else {
    console.log('请求失败')
    // eslint-disable-next-line
    mui.toast('请求失败')
  }
  return Promise.reject(err)
})
// 挂载实例
Vue.prototype.axios = http
// ========== axios ================ end

// 图片懒加载
Vue.use(VueLazyload, {
  loading: '/img/loading.gif'
})

new Vue({
  router,
  store,
  render: h => h(App),
  created () {
    // 初始化更新购物车
    this.$store.commit('cartInit')
  }
}).$mount('#app')
